import { SEARCH_TXT_MAP } from '../../constant/index';

Component({
  options: {
    addGlobalClass: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    searchTxt: String,
  },

  /**
   * 组件的初始数据
   */
  data: {
    searchBtn: SEARCH_TXT_MAP['cancel']
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onBindinput(e) {
      const { value } = e.detail || {};
      this.updateData(value);
      this.triggerEvent('onBindinput', value)
    },
    onDeleteInput() {
      this.updateData();
      this.triggerEvent('onBindinput', '')
    },
    onBindconfirm(e) {
      this.triggerEvent('onBindconfirm')
    },
    onConfirm() {
      const { searchBtn } = this.data;
      if (searchBtn.type === SEARCH_TXT_MAP['search'].type) {
        this.triggerEvent('onBindconfirm')
      } else if (searchBtn.type === SEARCH_TXT_MAP['cancel'].type) {
        wx.navigateBack({
          delta: 1
        });
      }
    },
    updateData(value = '') {
      this.setData({
        searchBtn: value ? SEARCH_TXT_MAP['search'] : SEARCH_TXT_MAP['cancel']
      })
    }
  }
})

